package com.atguigu.lease.web.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.mapper.*;
import com.atguigu.lease.web.service.*;
import com.atguigu.lease.web.vo.attr.AttrValueVo;
import com.atguigu.lease.web.vo.graph.GraphVo;
import com.atguigu.lease.web.vo.room.RoomDetailVo;
import com.atguigu.lease.web.vo.room.RoomItemVo;
import com.atguigu.lease.web.vo.room.RoomQueryVo;
import com.atguigu.lease.web.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@Transactional
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private AttrValueMapper attrValueMapper;

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        Long roomId = roomSubmitVo.getId();
        // 判定是保存还是更新
        boolean isUpdate = roomId != null;
        // 保存或者更新房间信息
        this.saveOrUpdate(roomSubmitVo);
        // 如果是更新，删除中间表数据
        if (isUpdate) {
            // 图片
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM)
                                    .eq(GraphInfo::getItemId, roomId));
            // 配套
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomId));
            // 标签
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomId));
            // 支付方式
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomId));
            // 租期
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomId));
            // 属性
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomId));
        }
        // 中间表数据保存
        // 图片
        if (!CollectionUtils.isEmpty(roomSubmitVo.getGraphVoList())) {
            List<GraphInfo> graphInfoList = roomSubmitVo.getGraphVoList().stream().map(item -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(item.getUrl());
                graphInfo.setName(item.getName());
                graphInfo.setItemId(roomId);
                graphInfo.setItemType(ItemType.ROOM);
                return graphInfo;
            }).collect(Collectors.toList());
            graphInfoService.saveBatch(graphInfoList);
        }
        // 配套
        if (!CollectionUtils.isEmpty(roomSubmitVo.getFacilityInfoIds())) {
            List<RoomFacility> roomFacilities = roomSubmitVo.getFacilityInfoIds().stream().map(item -> {
                return RoomFacility.builder().facilityId(item).roomId(roomId).build();
            }).collect(Collectors.toList());
            roomFacilityService.saveBatch(roomFacilities);
        }
        // 标签
        if (!CollectionUtils.isEmpty(roomSubmitVo.getLabelInfoIds())) {
            List<RoomLabel> roomLabels = roomSubmitVo.getLabelInfoIds().stream().map(item -> {
                return RoomLabel.builder().labelId(item).roomId(roomId).build();
            }).collect(Collectors.toList());
            roomLabelService.saveBatch(roomLabels);
        }
        // 支付方式
        if (!CollectionUtils.isEmpty(roomSubmitVo.getPaymentTypeIds())) {
            List<RoomPaymentType> roomPaymentTypes = roomSubmitVo.getPaymentTypeIds().stream().map(item -> {
                return RoomPaymentType.builder().paymentTypeId(item).roomId(roomId).build();
            }).collect(Collectors.toList());
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        // 租期
        if (!CollectionUtils.isEmpty(roomSubmitVo.getLeaseTermIds())) {
            List<RoomLeaseTerm> roomLeaseTerms = roomSubmitVo.getLeaseTermIds().stream().map(item -> {
                return RoomLeaseTerm.builder().leaseTermId(item).roomId(roomId).build();
            }).collect(Collectors.toList());
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
        // 属性
        if (!CollectionUtils.isEmpty(roomSubmitVo.getAttrValueIds())) {
            List<RoomAttrValue> roomAttrValues = roomSubmitVo.getAttrValueIds().stream().map(item -> {
                return RoomAttrValue.builder().attrValueId(item).roomId(roomId).build();
            }).collect(Collectors.toList());
            roomAttrValueService.saveBatch(roomAttrValues);
        }
    }

    @Override
    public void customQueryPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        this.baseMapper.customQueryPage(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 基本信息
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        // 图片
        List<GraphVo> graphInfoList = graphInfoMapper.queryByItemId(id, ItemType.ROOM);
        roomDetailVo.setGraphVoList(graphInfoList);
        // 配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        // 标签
        List<LabelInfo> labelInfoList = labelInfoMapper.queryByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        // 支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.queryByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        // 租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.queryByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
        // 属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.queryByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        return roomDetailVo;
    }

    @Override
    public void customRemoveById(Long roomId) {
        // 基本信息
        this.removeById(roomId);
        // 图片
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getItemId, roomId));
        // 配套
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomId));
        // 标签
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomId));
        // 支付方式
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomId));
        // 租期
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomId));
        // 属性
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomId));
    }
}




